﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate;
using NHibernate.Cfg;
using GS.Cricket.Entities;
using NHibernate.Tool.hbm2ddl;
using GS.Cricket.Service;
using GS.Cricket.Service.Impl;
using System.Reflection;
using FizzWare.NBuilder.Generators;
using log4net;

namespace GS.Cricket.Test
{
    [TestClass]
    public class TeamServiceTest
    {
        private static readonly ILog log =
        LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private ISessionFactory _sessionFactory;
        private Configuration _configuration;

        [TestInitialize]
        public void Initialize()
        {

            #if DEBUG
                Console.SetOut(new Logging.CustomDebugWriter());
            #endif

            var cfg = new Configuration();
            cfg.Configure();
            cfg.AddAssembly(typeof(Match).Assembly);
            new SchemaExport(cfg).Drop(false, false);
            new SchemaExport(cfg).Execute(false, true, false);

        }


        [TestMethod]
        public void Add_can_add_Team()
        {
            var team = new Team
            {
                Name = GetRandom.Phrase(30),
                Captain = GetRandom.Int(),
                Group = GetRandom.Int(),
                Points = 11,
                RunRate = 2.11
            };

            ITeamService teamService = new TeamService();
            var teamId = teamService.Add(team);

            // use session to try to load the item

            using (ISession session = Logging.Helper<Entities.Team>.OpenSession())
            {
                var fromDb = session.Get<Entities.Team>(teamId);

                // Test that the item was successfully inserted
                Assert.IsNotNull(fromDb);
                Assert.AreNotSame(team, fromDb);
                Assert.AreEqual(team.Name, fromDb.Name);
            }
        }

    }
}
